package com.reyco1.media.audio
{
	import com.reyco1.interfaces.IPausable;
	
	import flash.events.Event;
	import flash.events.ProgressEvent;

	public class CueableSound extends CoreSound implements IPausable
	{
		private var cueManager:CuePointManager;
		public  var ommitCuePoints:Boolean; 
		public  var cuepointAccuracy:int;
		
		public function CueableSound(soundObject:*, properties:Object=null)
		{
			super(soundObject, properties);

			if(properties != null)
			{
				ommitCuePoints   = properties.ommitCuePoints   == undefined || properties.ommitCuePoints   == null ? false : properties.ommitCuePoints;
				cuepointAccuracy = properties.cuepointAccuracy == undefined || properties.cuepointAccuracy == null ? 100   : properties.cuepointAccuracy;
			}
			else
			{
				ommitCuePoints 	 = false;
				cuepointAccuracy = 100;
			}
			
			cueManager = new CuePointManager( cuepointAccuracy );
			
			addEventListener(ProgressEvent.PROGRESS, handlePlayProgress);
		}
		
		public function addCuePoint(time:Number, method:Function, params:Array = null):void
		{
			cueManager.add( time, method, params );
		}
		
		public function removeCuePoint(time:Number):void
		{
			cueManager.remove(time);
		}
		
		override public function clear():void
		{
			removeEventListener(ProgressEvent.PROGRESS, handlePlayProgress);
			
			super.clear();
			cueManager.clear();
			cueManager = null;
		}
		
		private function handlePlayProgress(event:Event):void
		{
			if(!ommitCuePoints) 
				cueManager.update( progress );
		}
	}
}